package com.gauravk.audiovisualizersample.ui;

import com.gauravk.audiovisualizer.visualizer.BlastVisualizer;
import com.gauravk.audiovisualizersample.ResourceTable;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.media.audio.AudioManager;
import ohos.media.common.Source;
import ohos.media.player.Player;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

public class MusicStreamAbility extends Ability {
    private Image imgButton;
    private BlastVisualizer mVisualizer;
    private Player player = new Player(MusicStreamAbility.this);
    private String stream = "http://stream.radioreklama.bg/radio1rock128";
    private int progress = 0;
    private Timer timer;
    private CommonDialog toastDialog;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_music_stream);

        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.rgb(66, 119, 208));

        mVisualizer = (BlastVisualizer) findComponentById(ResourceTable.Id_blast);
        imgButton = (Image) findComponentById(ResourceTable.Id_playbtn);
        imgButton.setPixelMap(ResourceTable.Media_play);
        mVisualizer.setAudioSessionId(AudioManager.makeSessionId());
        imgButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (player.isNowPlaying()) {
                    player.pause();
                    imgButton.setPixelMap(ResourceTable.Media_play);
                } else {
                    player.play();
                    imgButton.setPixelMap(ResourceTable.Media_stop);
                }
            }
        });
        openDialog();
        openNet();
    }

    private void openDialog() {
        Component component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_progress_layout, null, false);
        RoundProgressBar bar = (RoundProgressBar) component.findComponentById(ResourceTable.Id_bar);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                bar.setProgressValue(progress);
                progress += 10;
                if (progress == 100) {
                    progress = 0;
                }
            }
        }, 0, 100);
        toastDialog = new CommonDialog(this);
        toastDialog.setContentCustomComponent(component);
        toastDialog.setAutoClosable(true);
        toastDialog.setSize(DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes().width / 7 * 6,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        player.stop();
    }

    private void openNet() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream inputStream = null;
                try {
                    URL url = new URL(stream);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    inputStream = urlConnection.getInputStream();
                    Source source = new Source(stream);
                    player.setSource(source);
                    player.prepare();
                    timer.cancel();
                    toastDialog.remove();
                } catch (MalformedURLException e) {
                    Logger.getGlobal().finest(e.getMessage());
                } catch (IOException e) {
                    Logger.getGlobal().finest(e.getMessage());
                } finally {
                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        Logger.getGlobal().finest(e.getMessage());
                    }
                }
            }
        }).start();
    }
}
